package net.edwardstx.brian.rpn.types;

import net.edwardstx.brian.rpn.RPNStackItem;
import net.edwardstx.brian.rpn.exception.InvalidComparison;

@SuppressWarnings("rawtypes")
public abstract class RPNNumeric<T extends Number> implements RPNStackItem<T>, Comparable<RPNNumeric> {

	protected final T value;
	
	RPNNumeric(T value){
		this.value = value;
	}
	
	public T getValue(){ return value;}
	
	@Override
	public int hashCode() {
		final int prime = 1201;
		int result = this.getAssociatedType().hashCode();
		result = prime * result + value.hashCode();
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof RPNNumeric))
			return false;
		RPNNumeric other = (RPNNumeric) obj;
		try{
			return this.compareTo(other) == 0;
		} catch (InvalidComparison e) {}
		try{
			return other.compareTo(this) == 0;
		} catch (InvalidComparison e) {}
		return false;
	}
	

}
